//
//  OrderProdItemView.m
//  JiaoX
//
//  Created by yinqy on 15/1/15.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "OrderProdItemView.h"

#define PRICE_LABEL_FONT_SIZE [DimensUtils getScaleX:15]



@interface OrderProdItemView()
{
    UIImageView *_imageView;
    UILabel *_priceLabel;
    UILabel *_numLabel;
    UILabel *_prodNameLabel;
}

@end

@implementation OrderProdItemView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //头图
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(JXPublicInset, JXPublicInset, ORDER_ITEM_IMAGE_WIDTH, ORDER_ITEM_IMAGE_WIDTH)];
        
        [self addSubview:_imageView];
        
        _priceLabel = [[UILabel alloc]init];
        _priceLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:_priceLabel];
        
        //数量
        _numLabel = [[UILabel alloc]init];
        [self addSubview:_numLabel];
        
        //商品名
        _prodNameLabel = [[UILabel alloc]init];
        [_prodNameLabel setNumberOfLines:3];
        [_prodNameLabel setTextColor:JXColor(77, 77, 77)];
        [_prodNameLabel setFont:[UIFont systemFontOfSize:PRICE_LABEL_FONT_SIZE]];
        [self addSubview:_prodNameLabel];
        
    }
    return self;
}

-(void)setCartProdSub:(CartProdSub *)sub{
    _cartProdSub = sub;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:sub.prodImageUrl]];
    //价格
    NSString *priceStr = [NSString stringWithFormat:@"￥%@",sub.prodNewPrice];
    CGSize priceSize = [priceStr sizeWithFont:[UIFont systemFontOfSize:PRICE_LABEL_FONT_SIZE]];
    _priceLabel.frame = CGRectMake(JXScreenW-JXPublicInset-55, (ORDER_ITEM_VIEW_HEIGHT-priceSize.height)/2, 55, priceSize.height);
    [_priceLabel setText:priceStr];
    
    CGSize numSize = [[NSString stringWithFormat:@"%@",sub.prodCount] sizeWithFont:[UIFont systemFontOfSize:PRICE_LABEL_FONT_SIZE]];
    _numLabel.frame = CGRectMake(CGRectGetMinX(_priceLabel.frame)-numSize.width-16, _priceLabel.y, numSize.width,numSize.height);
    [_numLabel setText:[NSString stringWithFormat:@"%@",sub.prodCount]];
    
    [_prodNameLabel setFrame:CGRectMake(CGRectGetMaxX(_imageView.frame)+JXPublicInset, JXPublicInset, CGRectGetMinX(_numLabel.frame)-CGRectGetMaxX(_imageView.frame)-JXPublicInset*2, ORDER_ITEM_IMAGE_WIDTH)];
    if ([sub.isPresent integerValue] == 1) {
        //是赠品
        NSString *str = [NSString stringWithFormat:@"(赠)%@",sub.prodName];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:JXThemeColor forKey:NSStringFromRange(NSMakeRange(0, 3))];
        [_prodNameLabel setAttributedText:[Utils getAttribtString:str attrRangs:dic]];
    }else{
        [_prodNameLabel setText:sub.prodName];
    }
}


@end
